﻿namespace Magic.Oidc.AuthorizatonServer.Extensions
{
    public static class RegisterMiddlewares
    {
        public static WebApplication SetupMiddlewares(this WebApplication app)
        {
            using (var scope = app.Services.CreateScope())
            {
                var seeder = scope.ServiceProvider.GetRequiredService<ClientsSeeder>();

                seeder.AddOidcDebuggerClient().GetAwaiter().GetResult();

                seeder.AddClients().GetAwaiter().GetResult();
                seeder.AddScopes().GetAwaiter().GetResult();

                seeder.AddInitUsers().GetAwaiter().GetResult();
                seeder.AddMvcClient().GetAwaiter().GetResult();
            }
            
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }
            app.UseStaticFiles();
            app.UseHttpsRedirection();
            app.UseCors();
            app.UseAuthentication();
            app.UseAuthorization();
            app.MapControllers();
            app.MapRazorPages();
            return app;
        }
    }
}
